
var forms = {};
(function(){
	
	var request, htmlForm;
	
	var isUrl = function(url){
		var re = /^(https?:\/\/)?(([\w!~*'().&=+$%-]+: )?[\w!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([\w!~*'()-]+\.)*([\w^-][\w-]{0,61})?[\w]\.[a-z]{2,6})(:[0-9]{1,4})?((\/*)|(\/+[\w!~*'().;?:@&=+$,%#-]+)+\/*)$/
	  return re.test(url);
	}
	
	forms.init = function(r){
		request = r;
		htmlForm = new HTMLForm.init(r)
	}
	
	forms.login = function(){
		return htmlForm.create({
			id: "loginForm",
			method: "post",
			inputs: [
				{"username": {label: "username:", validate: {required: true}}},
				{"password": {label: "password:", type: "password", value: "", validate: {required: true}}},
				{"redirect": {type: "hidden", value: request.params.redirect || request.uri}},
				{submit_: {value: "Login", type: "submit"}}
			]
		});
	}
	
	forms.register = function(){
		return htmlForm.create({
			id: "registerForm",
			method: "post",
			inputs: [
				{"username": {label: "username:", validate: {required: true}}},
				{"password": {label: "password:", type: "password", value: "", validate: {required: true}}},
				{"verify-password_": {label: "verify-password:", type: "password", value: "", validate: {test: function(thisValue, formValues){return thisValue === formValues.password}}}},
				{submit_: {value: "Register", type: "submit"}}
			]
		});
	}
	
	forms.requestAnchor = function(){
		return htmlForm.create({
			id: "requestAnchor",
			method: "post",
			inputs: [
				{"href": {label: "Web Address:", value: request.params.href || "http://" , validate: {test: function(val){return isUrl(val)}}}},
				{submit_: {value: "Preview", type: "submit"}}
			]
		});
	}
	
	forms.previewAnchor = function(){
    return htmlForm.create({
      id: "previewAnchor",
      method: "post",
      inputs: [
        {"title": {label: "Title:", value: request.params.title || "", validate: {required: true}}},
        {"caption": {label: "Caption:", type: "textarea", value: request.params.caption || "", validate: {required: true}}},
        {submit_: {value: "Request Link", type: "submit"}}
      ]
    });
  }
	
	forms.editAnchor = function(link){
		return htmlForm.create({
			id: "editAnchorForm",
			method: "post",
			inputs: [
				{"href": {label: "href:", value: link.href, validate: {required: true}}},
				{submit_: {value: "Save", type: "submit"}}
			]
		});
	}		
	
})();
